package com.yfbao.horizon.schedule.tk.common;

import org.nustaq.serialization.FSTConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

public class FstSerializer implements RedisSerializer<Object> {
    private static final Logger log = LoggerFactory.getLogger(FstSerializer.class);
    private final FSTConfiguration fstConfiguration;

    public FstSerializer(FSTConfiguration fstConfiguration) {
        this.fstConfiguration = fstConfiguration;
    }


    @Override
    public byte[] serialize(Object o) throws SerializationException {
        if (o == null) {
            return new byte[0];
        }
        return fstConfiguration.asByteArray(o);
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
//        if (bytes == null || bytes.length == 0) {
//            return null;
//        }
        if (bytes == null ) {
            return null;
        }
        return fstConfiguration.asObject(bytes);
    }
}
